{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf9b3276",
   "metadata": {},
   "source": [
    "## 读取数据\n",
    "### 列名依次代表学校、年级、姓名、性别、身高、体重、是否为转系生、体测场次、测试时间、1000米成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41bdbf91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "      <th>Test_Number</th>\n",
       "      <th>Test_Date</th>\n",
       "      <th>Time_Record</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019/10/5</td>\n",
       "      <td>0:04:34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Peking University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019/9/4</td>\n",
       "      <td>0:04:20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Mei Sun</td>\n",
       "      <td>Male</td>\n",
       "      <td>188.9</td>\n",
       "      <td>89.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019/9/12</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2020/1/3</td>\n",
       "      <td>0:04:08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Gaojuan You</td>\n",
       "      <td>Male</td>\n",
       "      <td>174.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019/11/6</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Junior</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>153.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019/10/17</td>\n",
       "      <td>0:04:31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>196</th>\n",
       "      <td>Tsinghua University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Li Zhao</td>\n",
       "      <td>Female</td>\n",
       "      <td>160.9</td>\n",
       "      <td>50.0</td>\n",
       "      <td>N</td>\n",
       "      <td>3</td>\n",
       "      <td>2019/9/22</td>\n",
       "      <td>0:04:03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>197</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengqiang Chu</td>\n",
       "      <td>Female</td>\n",
       "      <td>153.9</td>\n",
       "      <td>45.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2020/1/5</td>\n",
       "      <td>0:04:48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengmei Shen</td>\n",
       "      <td>Male</td>\n",
       "      <td>175.3</td>\n",
       "      <td>71.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2020/1/7</td>\n",
       "      <td>0:04:58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>Tsinghua University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Chunpeng Lv</td>\n",
       "      <td>Male</td>\n",
       "      <td>155.7</td>\n",
       "      <td>51.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019/11/6</td>\n",
       "      <td>0:05:05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>200 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            School      Grade            Name  Gender  Height  \\\n",
       "0    Shanghai Jiao Tong University   Freshman    Gaopeng Yang  Female   158.9   \n",
       "1                Peking University   Freshman  Changqiang You    Male   166.5   \n",
       "2    Shanghai Jiao Tong University     Senior         Mei Sun    Male   188.9   \n",
       "3                 Fudan University  Sophomore    Xiaojuan Sun  Female     NaN   \n",
       "4                 Fudan University  Sophomore     Gaojuan You    Male   174.0   \n",
       "..                             ...        ...             ...     ...     ...   \n",
       "195               Fudan University     Junior    Xiaojuan Sun  Female   153.9   \n",
       "196            Tsinghua University     Senior         Li Zhao  Female   160.9   \n",
       "197  Shanghai Jiao Tong University     Senior  Chengqiang Chu  Female   153.9   \n",
       "198  Shanghai Jiao Tong University     Senior   Chengmei Shen    Male   175.3   \n",
       "199            Tsinghua University  Sophomore     Chunpeng Lv    Male   155.7   \n",
       "\n",
       "     Weight Transfer  Test_Number   Test_Date Time_Record  \n",
       "0      46.0        N            1   2019/10/5     0:04:34  \n",
       "1      70.0        N            1    2019/9/4     0:04:20  \n",
       "2      89.0        N            2   2019/9/12     0:05:22  \n",
       "3      41.0        N            2    2020/1/3     0:04:08  \n",
       "4      74.0        N            2   2019/11/6     0:05:22  \n",
       "..      ...      ...          ...         ...         ...  \n",
       "195    46.0        N            2  2019/10/17     0:04:31  \n",
       "196    50.0        N            3   2019/9/22     0:04:03  \n",
       "197    45.0        N            1    2020/1/5     0:04:48  \n",
       "198    71.0        N            2    2020/1/7     0:04:58  \n",
       "199    51.0        N            1   2019/11/6     0:05:05  \n",
       "\n",
       "[200 rows x 10 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('data/learn_pandas.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e98e3325",
   "metadata": {},
   "source": [
    "## 只取前7列数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ee0aa43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Peking University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Mei Sun</td>\n",
       "      <td>Male</td>\n",
       "      <td>188.9</td>\n",
       "      <td>89.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Gaojuan You</td>\n",
       "      <td>Male</td>\n",
       "      <td>174.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Junior</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>153.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>196</th>\n",
       "      <td>Tsinghua University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Li Zhao</td>\n",
       "      <td>Female</td>\n",
       "      <td>160.9</td>\n",
       "      <td>50.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>197</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengqiang Chu</td>\n",
       "      <td>Female</td>\n",
       "      <td>153.9</td>\n",
       "      <td>45.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengmei Shen</td>\n",
       "      <td>Male</td>\n",
       "      <td>175.3</td>\n",
       "      <td>71.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>Tsinghua University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Chunpeng Lv</td>\n",
       "      <td>Male</td>\n",
       "      <td>155.7</td>\n",
       "      <td>51.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>200 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            School      Grade            Name  Gender  Height  \\\n",
       "0    Shanghai Jiao Tong University   Freshman    Gaopeng Yang  Female   158.9   \n",
       "1                Peking University   Freshman  Changqiang You    Male   166.5   \n",
       "2    Shanghai Jiao Tong University     Senior         Mei Sun    Male   188.9   \n",
       "3                 Fudan University  Sophomore    Xiaojuan Sun  Female     NaN   \n",
       "4                 Fudan University  Sophomore     Gaojuan You    Male   174.0   \n",
       "..                             ...        ...             ...     ...     ...   \n",
       "195               Fudan University     Junior    Xiaojuan Sun  Female   153.9   \n",
       "196            Tsinghua University     Senior         Li Zhao  Female   160.9   \n",
       "197  Shanghai Jiao Tong University     Senior  Chengqiang Chu  Female   153.9   \n",
       "198  Shanghai Jiao Tong University     Senior   Chengmei Shen    Male   175.3   \n",
       "199            Tsinghua University  Sophomore     Chunpeng Lv    Male   155.7   \n",
       "\n",
       "     Weight Transfer  \n",
       "0      46.0        N  \n",
       "1      70.0        N  \n",
       "2      89.0        N  \n",
       "3      41.0        N  \n",
       "4      74.0        N  \n",
       "..      ...      ...  \n",
       "195    46.0        N  \n",
       "196    50.0        N  \n",
       "197    45.0        N  \n",
       "198    71.0        N  \n",
       "199    51.0        N  \n",
       "\n",
       "[200 rows x 7 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=df[df.columns[:7]]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9354d2ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Peking University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Mei Sun</td>\n",
       "      <td>Male</td>\n",
       "      <td>188.9</td>\n",
       "      <td>89.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Fudan University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Gaojuan You</td>\n",
       "      <td>Male</td>\n",
       "      <td>174.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          School      Grade            Name  Gender  Height  \\\n",
       "0  Shanghai Jiao Tong University   Freshman    Gaopeng Yang  Female   158.9   \n",
       "1              Peking University   Freshman  Changqiang You    Male   166.5   \n",
       "2  Shanghai Jiao Tong University     Senior         Mei Sun    Male   188.9   \n",
       "3               Fudan University  Sophomore    Xiaojuan Sun  Female     NaN   \n",
       "4               Fudan University  Sophomore     Gaojuan You    Male   174.0   \n",
       "\n",
       "   Weight Transfer  \n",
       "0    46.0        N  \n",
       "1    70.0        N  \n",
       "2    89.0        N  \n",
       "3    41.0        N  \n",
       "4    74.0        N  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()#默认返回前5行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4fbed92d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Peking University</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          School     Grade            Name  Gender  Height  \\\n",
       "0  Shanghai Jiao Tong University  Freshman    Gaopeng Yang  Female   158.9   \n",
       "1              Peking University  Freshman  Changqiang You    Male   166.5   \n",
       "\n",
       "   Weight Transfer  \n",
       "0    46.0        N  \n",
       "1    70.0        N  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(2)# 前2行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5fec7dfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>197</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengqiang Chu</td>\n",
       "      <td>Female</td>\n",
       "      <td>153.9</td>\n",
       "      <td>45.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>198</th>\n",
       "      <td>Shanghai Jiao Tong University</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Chengmei Shen</td>\n",
       "      <td>Male</td>\n",
       "      <td>175.3</td>\n",
       "      <td>71.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199</th>\n",
       "      <td>Tsinghua University</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Chunpeng Lv</td>\n",
       "      <td>Male</td>\n",
       "      <td>155.7</td>\n",
       "      <td>51.0</td>\n",
       "      <td>N</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            School      Grade            Name  Gender  Height  \\\n",
       "197  Shanghai Jiao Tong University     Senior  Chengqiang Chu  Female   153.9   \n",
       "198  Shanghai Jiao Tong University     Senior   Chengmei Shen    Male   175.3   \n",
       "199            Tsinghua University  Sophomore     Chunpeng Lv    Male   155.7   \n",
       "\n",
       "     Weight Transfer  \n",
       "197    45.0        N  \n",
       "198    71.0        N  \n",
       "199    51.0        N  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(3) # 后3行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "503ca67a",
   "metadata": {},
   "source": [
    "## 使用info返回概况\n",
    "### 比如Height → 有 183 个非空，而数据一共有200行，说明有 17 个缺失值，数据类型是 float64。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7363db64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 200 entries, 0 to 199\n",
      "Data columns (total 7 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   School    200 non-null    object \n",
      " 1   Grade     200 non-null    object \n",
      " 2   Name      200 non-null    object \n",
      " 3   Gender    200 non-null    object \n",
      " 4   Height    183 non-null    float64\n",
      " 5   Weight    189 non-null    float64\n",
      " 6   Transfer  188 non-null    object \n",
      "dtypes: float64(2), object(5)\n",
      "memory usage: 11.1+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db58745c",
   "metadata": {},
   "source": [
    "## df.describe()\n",
    "### 默认情况下 df.describe() 只会统计数值型列\n",
    "- count\n",
    "非空值的数量\n",
    "Height 有 183 个值（说明有缺失），Weight 有 189 个值\n",
    "- mean\n",
    "平均值（算术平均数）\n",
    "- std\n",
    "标准差（standard deviation）\n",
    "衡量数据离散程度\n",
    "身高的标准差 ≈ 8.61，体重的标准差 ≈ 12.82 → 体重的波动更大\n",
    "- min\n",
    "最小值\n",
    "- 25%\n",
    "第一四分位数（Q1，25% 的数据 ≤ 这个值）\n",
    "- 50%（中位数）\n",
    "一半数据 ≤ 这个值\n",
    "身高：161.9 cm，体重：51 kg\n",
    "- 75%\n",
    "第三四分位数（Q3，75% 的数据 ≤ 这个值）\n",
    "- max\n",
    "最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "23ded7cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>183.000000</td>\n",
       "      <td>189.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>163.218033</td>\n",
       "      <td>55.015873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.608879</td>\n",
       "      <td>12.824294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>145.400000</td>\n",
       "      <td>34.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>157.150000</td>\n",
       "      <td>46.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>161.900000</td>\n",
       "      <td>51.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>167.500000</td>\n",
       "      <td>65.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>193.900000</td>\n",
       "      <td>89.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Height      Weight\n",
       "count  183.000000  189.000000\n",
       "mean   163.218033   55.015873\n",
       "std      8.608879   12.824294\n",
       "min    145.400000   34.000000\n",
       "25%    157.150000   46.000000\n",
       "50%    161.900000   51.000000\n",
       "75%    167.500000   65.000000\n",
       "max    193.900000   89.000000"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "827eb643",
   "metadata": {},
   "source": [
    "## 在 Series 和 DataFrame 上定义了许多统计函数\n",
    "### 最常见的是 sum, mean, median, var, std, max, min。 这里只演示一个 mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ca0cdefb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Height    163.218033\n",
      "Weight     55.015873\n",
      "dtype: float64\n",
      "H Mean:163.21803278688526\n"
     ]
    }
   ],
   "source": [
    "df_demo=df[['Height','Weight']]\n",
    "mean=df_demo.mean()\n",
    "print(mean)\n",
    "print(\"H Mean:\"+str(mean[\"Height\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b0785cd",
   "metadata": {},
   "source": [
    "## 聚合函数默认是按列聚合的，即axis=0\n",
    "### 如果传入axis=1，表示按行聚合。 在这个例子中，把每行的(身高+体重)/2 来算平均，得出来的数是没有意义的，只是测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "27949bf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      102.45\n",
       "1      118.25\n",
       "2      138.95\n",
       "3       41.00\n",
       "4      124.00\n",
       "        ...  \n",
       "195     99.95\n",
       "196    105.45\n",
       "197     99.45\n",
       "198    123.15\n",
       "199    103.35\n",
       "Length: 200, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_demo.mean(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88241f20",
   "metadata": {},
   "source": [
    "## 对序列使用唯一值函数 unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "938d66db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Shanghai Jiao Tong University', 'Peking University',\n",
       "       'Fudan University', 'Tsinghua University'], dtype=object)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['School'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f642bf4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['School'].nunique() #唯一值的个数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04d016ef",
   "metadata": {},
   "source": [
    "### value_counts 可以得到唯一值和其对应出现的频数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e710041b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "School\n",
       "Tsinghua University              69\n",
       "Shanghai Jiao Tong University    57\n",
       "Fudan University                 40\n",
       "Peking University                34\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['School'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae22e201",
   "metadata": {},
   "source": [
    "## 替换函数\n",
    "### 一般而言，替换操作是针对某一个列进行的，因此下面的例子都以 Series 举例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0d63b711",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_38696\\677635475.py:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  df['Gender'].replace({'Female':0,'Male':1})\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0      0\n",
       "1      1\n",
       "2      1\n",
       "3      0\n",
       "4      1\n",
       "      ..\n",
       "195    0\n",
       "196    0\n",
       "197    0\n",
       "198    1\n",
       "199    1\n",
       "Name: Gender, Length: 200, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Gender'].replace({'Female':0,'Male':1})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc42c18",
   "metadata": {},
   "source": [
    "### 上面的语句，也可以写为：\n",
    "#### 最后的.head() 不会影响结果本身，只是方便预览"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cc155dc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\admin\\AppData\\Local\\Temp\\ipykernel_38696\\337556944.py:1: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  new_df=df['Gender'].replace(['Female','Male'],[0,1]).head()\n"
     ]
    }
   ],
   "source": [
    "new_df=df['Gender'].replace(['Female','Male'],[0,1]).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9751db60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    1\n",
       "3    0\n",
       "4    1\n",
       "Name: Gender, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_df.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3aa7ce45",
   "metadata": {},
   "source": [
    "## where 和 mask 替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8d5f343b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    -1.0\n",
       "1     NaN\n",
       "2     NaN\n",
       "3   -50.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series([-1,1.234,100,-50])\n",
    "s.where(s<0) # 注意这里和sql语句是反的，它的意思是找出 s<0 为 False的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "61eb9f88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    -1.0\n",
       "1     0.0\n",
       "2     0.0\n",
       "3   -50.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1=s.where(s<0,0) # 找到<0不成立的数，即>=0的， 将它们替换为0\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68a9cfa5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      0.000\n",
       "1      1.234\n",
       "2    100.000\n",
       "3      0.000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2=s.mask(s<0,0) # 找到 小于0的数，将它们替换为0\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63d37848",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
